
###############################################################################
# MODULE     : main TeXmacs make file
# COPYRIGHT  : (C) 1999-2008  Joris van der Hoeven
###############################################################################
# This software falls under the GNU general public license version 3 or later.
# It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
# in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
###############################################################################

prefix = @prefix@
exec_prefix = @exec_prefix@
includedir = @includedir@
libdir = @libdir@
bindir = @bindir@
datarootdir = @datarootdir@
datadir = @datadir@
mandir = @mandir@
tmdir = @tmdir@
tmbin = @tmbin@
tmdata = @tmdata@
tm_devel = @tm_devel@
tm_devel_release = @tm_devel_release@
tm_underscore_devel = @tm_underscore_devel@
tm_debian_name_devel = @tm_debian_name_devel@
tm_stable = @tm_stable@
tm_stable_release = @tm_stable_release@
tm_underscore_stable = @tm_underscore_stable@
tm_suffix = @CONFIG_OS_SUFFIX@
so = @CONFIG_SO@
GS_EXE= @GS_EXE@
GS_DLL= @GS_DLL@
DESTDIR =

MKDIR = mkdir -p
RM = rm -f
CP = cp -r -f
MV = mv -f
LN = ln -f
CHMOD = @CONFIG_CHMOD@
GZIP = gzip -f
STRIP = @CONFIG_STRIP@
TOUCH = touch

###############################################################################
# Main makes
###############################################################################

TEXMACS: EMPTY_DIRS
	cd src; $(MAKE) -f makefile link=shared
	$(MAKE) -f Makefile PLUGINS
	$(MAKE) -f Makefile EX_PLUGINS
	$(CP) misc/scripts/ifig2ps $(tmdir)/bin
	$(CP) misc/scripts/itexmacs $(tmdir)/bin
	$(CP) misc/scripts/tm_gs $(tmdir)/bin
	$(CHMOD) 755 $(tmdir)/bin/*
	$(CHMOD) 755 plugins/*/bin/*
	$(RM) -r $(tmdir)/plugins
	$(CP) plugins $(tmdir)/plugins
	@echo ----------------------------------------------------
	@echo dynamic TeXmacs has been successfully compiled

STATIC_TEXMACS: EMPTY_DIRS
	cd src; $(MAKE) -f makefile link=static
	$(MAKE) -f Makefile PLUGINS
	$(MAKE) -f Makefile EX_PLUGINS
	$(CP) misc/scripts/ifig2ps $(tmdir)/bin
	$(CP) misc/scripts/itexmacs $(tmdir)/bin
	$(CP) misc/scripts/tm_gs $(tmdir)/bin
	$(CHMOD) 755 $(tmdir)/bin/*
	$(CHMOD) 755 plugins/*/bin/*
	$(RM) -r $(tmdir)/plugins
	$(CP) plugins $(tmdir)/plugins
	@echo ----------------------------------------------------
	@echo static TeXmacs has been successfully compiled

DEPS: EMPTY_DIRS
	cd src; $(MAKE) -f makefile deps

EMPTY_DIRS:
	$(MKDIR) src/Deps
	$(MKDIR) src/Objects
	$(MKDIR) TeXmacs/bin
	$(MKDIR) TeXmacs/lib

GLUE:
	cd src; $(MAKE) -f makefile GLUE

.PHONY: TEXMACS STATIC_TEXMACS DEPS GLUE EXPERIMENTAL

deps: DEPS
install: INSTALL
uninstall: UNINSTALL
clean: CLEAN
distclean: DISTCLEAN

.PHONY: deps install uninstall clean distclean

###############################################################################
# Plugins
###############################################################################

PLUGINS_ALL := $(wildcard plugins/*)
PLUGINS_MAKEFILE := $(wildcard plugins/*/Makefile)
PLUGINS_COMPILE := $(patsubst %Makefile,%COMPILE,$(PLUGINS_MAKEFILE))
PLUGINS_CLEAN := $(patsubst %Makefile,%CLEAN,$(PLUGINS_MAKEFILE))

plugins/%/COMPILE:
	$(MKDIR) plugins/$*/bin
	cd plugins/$*; $(MAKE) -i -f Makefile CC="@CC@" CXX="@CXX@"

plugins/%/CLEAN:
	cd plugins/$*; $(MAKE) -i -f Makefile clean

PLUGINS: $(PLUGINS_COMPILE)

CLEAN_PLUGINS: $(PLUGINS_CLEAN)

.PHONY: PLUGINS CLEAN_PLUGINS

EX_PLUGINS_PRG := $(wildcard TeXmacs/examples/plugins/*/progs)
EX_PLUGINS_BIN := $(patsubst %/progs,%/bin,$(EX_PLUGINS_PRG))

TeXmacs/examples/plugins/%/bin:
	$(MKDIR) TeXmacs/examples/plugins/$*/bin

EX_PLUGINS: $(EX_PLUGINS_BIN)
	$(MKDIR) TeXmacs/examples/plugins/dynlink/lib

.PHONY: EX_PLUGINS

###############################################################################
# Installing and removing TeXmacs (for system administrators)
###############################################################################

INSTALL:
	$(MKDIR) $(DESTDIR)$(tmbin)
	$(MKDIR) $(DESTDIR)$(tmbin)/bin
	$(MKDIR) $(DESTDIR)$(tmbin)/lib
	$(CP) $(tmdir)/bin/texmacs.bin $(DESTDIR)$(tmbin)/bin
	$(CP) $(tmdir)/bin/tm_gs $(DESTDIR)$(tmbin)/bin
	$(CP) $(tmdir)/plugins/*/bin/* $(DESTDIR)$(tmbin)/bin
	$(CP) $(tmdir)/plugins/*/lib/*.$(so) \
	$(DESTDIR)$(tmbin)/lib 2>/dev/null || :
	$(CP) $(tmdir)/lib/*.$(so) $(DESTDIR)$(tmbin)/lib 2>/dev/null || :
	$(CHMOD) 755 $(DESTDIR)$(tmbin)/bin/*
	$(CHMOD) 755 $(DESTDIR)$(tmbin)/lib/*.$(so) 2>/dev/null || :
	$(RM) $(DESTDIR)$(tmbin)/lib/*.a
	@echo installed TeXmacs executables in $(DESTDIR)$(tmbin)
	$(MKDIR) $(DESTDIR)$(tmdata)
	$(CP) $(tmdir)/LICENSE $(DESTDIR)$(tmdata)
	$(CP) $(tmdir)/doc $(DESTDIR)$(tmdata)
	$(CP) $(tmdir)/examples $(DESTDIR)$(tmdata)
	$(CP) $(tmdir)/fonts $(DESTDIR)$(tmdata)
	$(CP) $(tmdir)/langs $(DESTDIR)$(tmdata)
	$(CP) $(tmdir)/misc $(DESTDIR)$(tmdata)
	$(CP) $(tmdir)/packages $(DESTDIR)$(tmdata)
	$(CP) $(tmdir)/progs $(DESTDIR)$(tmdata)
	$(CP) $(tmdir)/styles $(DESTDIR)$(tmdata)
	$(CP) $(tmdir)/texts $(DESTDIR)$(tmdata)
	$(CHMOD) -R go=rX $(DESTDIR)$(tmdata)
	@echo installed TeXmacs data in $(DESTDIR)$(tmdata)
	$(CP) plugins $(DESTDIR)$(tmdata)
	$(RM) $(DESTDIR)$(tmdata)/plugins/*/Makefile
	$(RM) -r $(DESTDIR)$(tmdata)/plugins/*/src
	$(RM) -r $(DESTDIR)$(tmdata)/plugins/*/bin
	$(RM) -r $(DESTDIR)$(tmdata)/plugins/*/lib
	@echo installed TeXmacs plugins data in $(DESTDIR)$(tmdata)/plugins
	$(MKDIR) $(DESTDIR)$(bindir)
	$(CHMOD) 755 $(tmdir)/bin/*
	$(CP) $(tmdir)/bin/ifig2ps $(DESTDIR)$(bindir)
	$(CP) $(tmdir)/bin/itexmacs $(DESTDIR)$(bindir)
	@echo installed TeXmacs startup scripts in $(DESTDIR)$(bindir)
	$(MKDIR) $(DESTDIR)$(includedir)
	$(CP) $(tmdir)/include/iTeXmacs.h $(DESTDIR)$(includedir)
	$(CHMOD) go=rX $(DESTDIR)$(includedir)/iTeXmacs.h
	@echo installed TeXmacs include files in $(DESTDIR)$(includedir)
	$(MKDIR) $(DESTDIR)$(mandir)
	$(MKDIR) $(DESTDIR)$(mandir)/man1
	$(CP) misc/man/ifig2ps.1 $(DESTDIR)$(mandir)/man1
	$(CP) misc/man/itexmacs.1 $(DESTDIR)$(mandir)/man1
	$(GZIP) $(DESTDIR)$(mandir)/man1/ifig2ps.1
	$(GZIP) $(DESTDIR)$(mandir)/man1/itexmacs.1
	$(CHMOD) go=rX $(DESTDIR)$(mandir)/man1/ifig2ps.1.gz
	$(CHMOD) go=rX $(DESTDIR)$(mandir)/man1/itexmacs.1.gz
	@echo installed TeXmacs manual pages in $(DESTDIR)$(mandir)
	@echo ----------------------------------------------------
	@echo TeXmacs has been successfully installed

UNINSTALL:
	$(RM) -r $(tmbin)
	@echo removed TeXmacs executables from $(tmbin)
	$(RM) -r $(tmdata)
	@echo removed TeXmacs data from $(tmdata)
	$(RM) $(includedir)/iTeXmacs.h
	@echo removed TeXmacs include files from $(includedir)
	$(RM) $(bindir)/ifig2ps
	$(RM) $(bindir)/itexmacs
	@echo removed TeXmacs startup scripts from $(bindir)
	$(RM) $(mandir)/man1/ifig2ps.1.gz
	$(RM) $(mandir)/man1/itexmacs.1.gz
	@echo removed TeXmacs manual pages from $(mandir)
	@echo ----------------------------------------------------
	@echo TeXmacs has been successfully removed

.PHONY: INSTALL UNINSTALL

###############################################################################
# Subtargets for the production of packages
###############################################################################

BUILD_DIR = ../distr/build/$(tm_devel)
BUILD_TGZ = ../distr/build/$(tm_devel).tar.gz

COPY_SOURCES:
	$(MKDIR) ../distr
	$(RM) -r ../distr/build
	$(MKDIR) ../distr/build
	$(MKDIR) $(BUILD_DIR)
	$(CP) -p * $(BUILD_DIR)/.
	cd $(BUILD_DIR); make ACCESS_FLAGS
	cd $(BUILD_DIR); make DISTR_CLEAN

COPY_SOURCES_TGZ: COPY_SOURCES
	cd ../distr/build; tar -czf $(tm_devel).tar.gz $(tm_devel) 

COPY_GUILE:
	GUILE_DATA_PATH=`guile-config info pkgdatadir`; \
	export GUILE_DATA_PATH; \
	GUILE_LOAD_PATH=`find $$GUILE_DATA_PATH -type d | grep ice-9`; \
	export GUILE_LOAD_PATH; \
	for I in $$GUILE_LOAD_PATH ; \
	do $(CP) $$I $(BUILD_DIR)/TeXmacs/progs/ ; done
	$(CHMOD) -R go=rX $(BUILD_DIR)/TeXmacs/progs/ice-9

###############################################################################
# Make a source package
###############################################################################

SRC_PACKAGE_DIR = ../distr/source

SRC_PACKAGE: COPY_SOURCES
	$(MV) $(BUILD_DIR) ../distr/build/$(tm_devel)-src
	cd ../distr/build; tar -czf $(tm_devel)-src.tar.gz $(tm_devel)-src
	$(MKDIR) $(SRC_PACKAGE_DIR)
	$(MV) ../distr/build/$(tm_devel)-src.tar.gz $(SRC_PACKAGE_DIR)/.
	$(RM) -r ../distr/build

.PHONY: SRC_PACKAGE

###############################################################################
# Make generic static binary packages
###############################################################################

GENERIC_PACKAGE_DIR = ../distr/generic
STATIC_QT = $(tm_devel)-$(tm_suffix)
STATIC_X11 = $(tm_devel)-x11-$(tm_suffix)

GENERIC_PACKAGE: COPY_SOURCES COPY_GUILE
	cd $(BUILD_DIR); ./configure
	cd $(BUILD_DIR); make STATIC_TEXMACS
	cd $(BUILD_DIR); make ACCESS_FLAGS
	$(MV) $(BUILD_DIR)/TeXmacs $(BUILD_DIR)/$(STATIC_QT)
	cd $(BUILD_DIR); tar -czf $(STATIC_QT).tar.gz $(STATIC_QT)
	$(MKDIR) $(GENERIC_PACKAGE_DIR)
	$(MV) $(BUILD_DIR)/$(STATIC_QT).tar.gz $(GENERIC_PACKAGE_DIR)
	$(RM) -r ../distr/build

GENERIC_X11_PACKAGE: COPY_SOURCES COPY_GUILE
	cd $(BUILD_DIR); ./configure --disable-qt
	cd $(BUILD_DIR); make STATIC_TEXMACS
	cd $(BUILD_DIR); make ACCESS_FLAGS
	$(MV) $(BUILD_DIR)/TeXmacs $(BUILD_DIR)/$(STATIC_X11)
	cd $(BUILD_DIR); tar -czf $(STATIC_X11).tar.gz $(STATIC_X11)
	$(MKDIR) $(GENERIC_PACKAGE_DIR)
	$(MV) $(BUILD_DIR)/$(STATIC_X11).tar.gz $(GENERIC_PACKAGE_DIR)
	$(RM) -r ../distr/build

.PHONY: GENERIC_PACKAGE GENERIC_X11_PACKAGE

###############################################################################
# Make a CentOs package
###############################################################################

CENTOS_PACKAGE_SRC = packages/centos
CENTOS_PACKAGE_DIR = ../distr/centos

CENTOS_PACKAGE: COPY_SOURCES_TGZ
	mkdir -p $(HOME)/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
	if test ! -f ~/.rpmmacros; then \
	  echo '%_topdir %(echo $$HOME)/rpmbuild' > ~/.rpmmacros; \
	fi
	$(CP) $(CENTOS_PACKAGE_SRC)/TeXmacs.spec $(HOME)/rpmbuild/SPECS/.
	$(CP) $(BUILD_TGZ) $(HOME)/rpmbuild/SOURCES/.
	cd $(HOME); rpmbuild -ba rpmbuild/SPECS/TeXmacs.spec
	$(MV) $(HOME)/rpmbuild/RPMS/*/TeXmacs* $(CENTOS_PACKAGE_DIR)
	$(MV) $(HOME)/rpmbuild/SRPMS/TeXmacs* $(CENTOS_PACKAGE_DIR)
	$(RM) -r ../distr/build

.PHONY: CENTOS_PACKAGE

###############################################################################
# Make a Debian package
###############################################################################

DEBIAN_PACKAGE_SRC = packages/debian
DEBIAN_PACKAGE_DIR = ../distr/debian

DEBIAN_PACKAGE: COPY_SOURCES_TGZ
	$(CP) $(BUILD_TGZ) ../distr/build/$(tm_debian_name_devel).orig.tar.gz
	$(MKDIR) $(BUILD_DIR)/debian ;
	$(CP) $(DEBIAN_PACKAGE_SRC)/* $(BUILD_DIR)/debian
	cd $(BUILD_DIR); debuild -us -uc
	$(CP) -p ../distr/build/*.deb $(DEBIAN_PACKAGE_DIR)
	$(RM) -r ../distr/build

.PHONY: DEBIAN_PACKAGE

###############################################################################
# Make a Fedora package
###############################################################################

FEDORA_PACKAGE_SRC = packages/fedora
FEDORA_PACKAGE_DIR = ../distr/fedora

FEDORA_PACKAGE: COPY_SOURCES_TGZ
	rpmdev-setuptree;
	$(CP) $(FEDORA_PACKAGE_SRC)/TeXmacs.spec $(HOME)/rpmbuild/SPECS/.
	$(CP) $(BUILD_TGZ) $(HOME)/rpmbuild/SOURCES/.
	cd $(HOME); rpmbuild -ba rpmbuild/SPECS/TeXmacs.spec
	$(MV) $(HOME)/rpmbuild/RPMS/*/TeXmacs* $(FEDORA_PACKAGE_DIR)
	$(MV) $(HOME)/rpmbuild/SRPMS/TeXmacs* $(FEDORA_PACKAGE_DIR)
	$(RM) -r ../distr/build

.PHONY: FEDORA_PACKAGE

###############################################################################
# Make a Mandriva package
###############################################################################

MANDRIVA_PACKAGE_SRC = packages/mandriva
MANDRIVA_PACKAGE_DIR = ../distr/mandriva

MANDRIVA_PACKAGE: COPY_SOURCES_TGZ
	mkdir -p $(HOME)/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
	if test ! -f ~/.rpmmacros; then \
	  echo '%_topdir %(echo $$HOME)/rpmbuild' > ~/.rpmmacros; \
	fi
	$(CP) $(MANDRIVA_PACKAGE_SRC)/TeXmacs.spec $(HOME)/rpmbuild/SPECS/.
	$(CP) $(BUILD_TGZ) $(HOME)/rpmbuild/SOURCES/.
	cd $(HOME); rpmbuild -ba rpmbuild/SPECS/TeXmacs.spec
	$(MV) $(HOME)/rpmbuild/RPMS/*/TeXmacs* $(MANDRIVA_PACKAGE_DIR)
	$(MV) $(HOME)/rpmbuild/SRPMS/TeXmacs* $(MANDRIVA_PACKAGE_DIR)
	$(RM) -r ../distr/build

.PHONY: MANDRIVA_PACKAGE

###############################################################################
# Make a RedHat package
###############################################################################

REDHAT_PACKAGE_SRC = packages/redhat
REDHAT_PACKAGE_DIR = ../distr/redhat

REDHAT_PACKAGE: COPY_SOURCES_TGZ
	mkdir -p $(HOME)/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
	if test ! -f ~/.rpmmacros; then \
	  echo '%_topdir %(echo $$HOME)/rpmbuild' > ~/.rpmmacros; \
	fi
	$(CP) $(REDHAT_PACKAGE_SRC)/TeXmacs.spec $(HOME)/rpmbuild/SPECS/.
	$(CP) $(BUILD_TGZ) $(HOME)/rpmbuild/SOURCES/.
	cd $(HOME); rpmbuild -ba rpmbuild/SPECS/TeXmacs.spec
	$(MKDIR) $(REDHAT_PACKAGE_DIR)
	$(MV) $(HOME)/rpmbuild/RPMS/*/TeXmacs* $(REDHAT_PACKAGE_DIR)
	$(MV) $(HOME)/rpmbuild/SRPMS/TeXmacs* $(REDHAT_PACKAGE_DIR)
	$(RM) -r ../distr/build

.PHONY: REDHAT_PACKAGE

###############################################################################
# Make a Ubuntu package
###############################################################################

UBUNTU_PACKAGE_SRC = packages/debian
UBUNTU_PACKAGE_DIR = ../distr/ubuntu

UBUNTU_PACKAGE: COPY_SOURCES_TGZ
	$(CP) $(BUILD_TGZ) ../distr/build/$(tm_debian_name_devel).orig.tar.gz
	$(MKDIR) $(BUILD_DIR)/debian ;
	$(CP) $(UBUNTU_PACKAGE_SRC)/* $(BUILD_DIR)/debian
	cd $(BUILD_DIR); debuild -us -uc
	$(CP) -p ../distr/build/*.deb $(UBUNTU_PACKAGE_DIR)
	$(RM) -r ../distr/build

.PHONY: UBUNTU_PACKAGE

###############################################################################
# Make Mac OS X bundles and diskimages
###############################################################################

MACOS_PACKAGE_SRC = packages/macos
MACOS_PACKAGE_DIR = ../distr/macos

MACOS_PACKAGE_APP = ../distr/$(tm_devel).app
MACOS_PACKAGE_DMG = ../distr/$(tm_devel).dmg
MACOS_PACKAGE_CONTENTS = $(MACOS_PACKAGE_APP)/Contents
MACOS_PACKAGE_RESOURCES = $(MACOS_PACKAGE_CONTENTS)/Resources
MACOS_PACKAGE_TEXMACS = $(MACOS_PACKAGE_RESOURCES)/share/TeXmacs

MACOS_BUNDLE: TEXMACS
	$(MKDIR) ../distr
	$(RM) -r $(MACOS_PACKAGE_APP)
	$(MKDIR) $(MACOS_PACKAGE_APP)
	$(MKDIR) $(MACOS_PACKAGE_CONTENTS)
	$(CP) $(MACOS_PACKAGE_SRC)/Info.plist $(MACOS_PACKAGE_CONTENTS)
	$(CP) $(MACOS_PACKAGE_SRC)/PkgInfo $(MACOS_PACKAGE_CONTENTS)
	$(MKDIR) $(MACOS_PACKAGE_CONTENTS)/MacOS
	$(CP) TeXmacs/bin/texmacs.bin $(MACOS_PACKAGE_CONTENTS)/MacOS/TeXmacs
	$(MKDIR) $(MACOS_PACKAGE_RESOURCES)
	$(CP) $(MACOS_PACKAGE_SRC)/iTeXmacs.icns $(MACOS_PACKAGE_RESOURCES)
	$(CP) $(MACOS_PACKAGE_SRC)/iTeXmacs-document.icns \
	$(MACOS_PACKAGE_RESOURCES)
	$(CP) src/Plugins/Cocoa/English.lproj $(MACOS_PACKAGE_RESOURCES)
	$(MKDIR) $(MACOS_PACKAGE_RESOURCES)/bin
	$(MKDIR) $(MACOS_PACKAGE_RESOURCES)/lib
	$(MKDIR) $(MACOS_PACKAGE_RESOURCES)/share
	$(CP) TeXmacs $(MACOS_PACKAGE_RESOURCES)/share
	$(RM) $(MACOS_PACKAGE_TEXMACS)/bin/texmacs.bin
	GUILE_DATA_PATH=`guile-config info pkgdatadir`; \
	export GUILE_DATA_PATH; \
	GUILE_LOAD_PATH=`find $$GUILE_DATA_PATH -type d | grep ice-9`; \
	export GUILE_LOAD_PATH; \
	$(CP) $$GUILE_LOAD_PATH $(MACOS_PACKAGE_TEXMACS)/progs/
	$(CHMOD) -R go=rX $(MACOS_PACKAGE_TEXMACS)/progs/ice-9
	$(RM) -r $(MACOS_PACKAGE_TEXMACS)/.svn
	$(RM) -r $(MACOS_PACKAGE_TEXMACS)/*/.svn
	$(RM) -r $(MACOS_PACKAGE_TEXMACS)/*/*/.svn
	$(RM) -r $(MACOS_PACKAGE_TEXMACS)/*/*/*/.svn
	$(RM) -r $(MACOS_PACKAGE_TEXMACS)/*/*/*/*/.svn
	$(RM) -r $(MACOS_PACKAGE_TEXMACS)/*/*/*/*/*/.svn
	$(RM) -r $(MACOS_PACKAGE_TEXMACS)/*/*/*/*/*/*/.svn
	$(RM) -r $(MACOS_PACKAGE_TEXMACS)/*/*/*/*/*/*/*/.svn
	$(MACOS_PACKAGE_SRC)/bundle-libs.sh \
	$(MACOS_PACKAGE_CONTENTS)/MacOS/TeXmacs

MACOS_PACKAGE: MACOS_BUNDLE
	$(RM) $(MACOS_PACKAGE_DMG)
	hdiutil create -srcfolder $(MACOS_PACKAGE_APP) $(MACOS_PACKAGE_DMG)
	$(MKDIR) $(MACOS_PACKAGE_DIR)
	$(MV) $(MACOS_PACKAGE_DMG) $(MACOS_PACKAGE_DIR)
	$(RM) -r $(MACOS_PACKAGE_APP)

BUNDLE: MACOS_BUNDLE

.PHONY: BUNDLE MACOS_BUNDLE MACOS_PACKAGE

###############################################################################
# Make a Windows installer
###############################################################################

WINDOWS_PACKAGE_SRC = packages/windows
WINDOWS_PACKAGE_DIR = ../distr/windows

WINDOWS_BUILD_DIR = ../distr/TeXmacs-Windows
WINDOWS_BUILD_BIN_DIR = $(WINDOWS_BUILD_DIR)/bin

## WARNING: the following path list needs to be adapted manually 
DLLPATH = $(PATH)
DLLS_IN_PATH = $(wildcard $(addsuffix /$(1),$(subst :, ,$(DLLPATH))))
DLL_LIST = QtCore4.dll QtGui4.dll QtSvg4.dll mingwm10.dll libgmp*.dll libguile*.dll libfreetype*.dll libltdl*.dll libiconv*.dll $(GS_EXE) $(GS_DLL) wget.exe
DLLS = $(foreach DLL,$(DLL_LIST),$(call DLLS_IN_PATH,$(DLL)))

WINDOWS_BUNDLE: TEXMACS
	$(MKDIR) ../distr
	$(RM) -r $(WINDOWS_BUILD_DIR)
	$(MKDIR) $(WINDOWS_BUILD_DIR)
	$(CP) TeXmacs/* $(WINDOWS_BUILD_DIR)/.
	$(MV) $(WINDOWS_BUILD_DIR)/bin/texmacs.bin \
	$(WINDOWS_BUILD_DIR)/bin/texmacs.exe
	$(RM) -r $(WINDOWS_BUILD_DIR)/bin/texmacs
	GUILE_DATA_PATH=`guile-config info pkgdatadir`; \
	export GUILE_DATA_PATH; \
	GUILE_LOAD_PATH=`find $$GUILE_DATA_PATH -type d | grep ice-9`; \
	export GUILE_LOAD_PATH; \
	for I in $$GUILE_LOAD_PATH ; \
	do $(CP) $$I $(WINDOWS_BUILD_DIR)/progs/ ; done
	$(CHMOD) -R go=rX $(WINDOWS_BUILD_DIR)/progs/ice-9
	$(RM) -r $(WINDOWS_BUILD_DIR)/.svn
	$(RM) -r $(WINDOWS_BUILD_DIR)/*/.svn
	$(RM) -r $(WINDOWS_BUILD_DIR)/*/*/.svn
	$(RM) -r $(WINDOWS_BUILD_DIR)/*/*/*/.svn
	$(RM) -r $(WINDOWS_BUILD_DIR)/*/*/*/*/.svn
	$(RM) -r $(WINDOWS_BUILD_DIR)/*/*/*/*/*/.svn
	$(RM) -r $(WINDOWS_BUILD_DIR)/*/*/*/*/*/*/.svn
	$(RM) -r $(WINDOWS_BUILD_DIR)/*/*/*/*/*/*/*/.svn
	for DLL in $(DLLS) ; do $(CP) $$DLL $(WINDOWS_BUILD_BIN_DIR) ; done

WINDOWS_PACKAGE: WINDOWS_BUNDLE
	iscc $(WINDOWS_PACKAGE_SRC)/TeXmacs.iss
	$(MKDIR) $(WINDOWS_PACKAGE_DIR)
	$(MV) ../distr/$(tm_devel)* $(WINDOWS_PACKAGE_DIR)
	#$(RM) -r $(WINDOWS_BUILD_DIR)

.PHONY: WINDOWS_BUNDLE WINDOWS_PACKAGE

###############################################################################
# Cleaning and backups
###############################################################################

RDISTR:
	$(RM) $(tmdir)/TEX_PATHS
	$(RM) *~
	$(RM) */*~
	$(RM) */*/*~
	$(RM) */*/*/*~
	$(RM) */*/*/*/*~
	$(RM) */*/*/*/*/*~
	$(RM) core
	$(RM) */core
	$(RM) src/*/core
	$(RM) src/*/*/core
	$(RM) src/*/*/*/core
	$(RM) misc/*/core
	$(RM) plugins/*/core
	$(RM) plugins/*/*/core
	$(RM) $(tmdir)/lib/*.a
	$(RM) $(tmdir)/fonts/error/* 2>/dev/null || :
	$(RM) -r autom*.cache

DISTR: RDISTR
	cd src; $(MAKE) -f makefile DISTR

RCLEAN: RDISTR
	$(RM) $(tmdir)/examples/plugins/bin/* 2>/dev/null || :
	$(RM) -r $(tmdir)/plugins
	$(RM) $(tmdir)/lib/* 2>/dev/null || :
	$(RM) $(tmdir)/bin/* 2>/dev/null || :
	$(RM) -r $(tmdir)/misc/images/.xvpics
	$(RM) -r $(tmdir)/progs/ice-9
	$(RM) -r X11

SCLEAN:
	cd src; $(MAKE) -f makefile CLEAN

CLEAN: SCLEAN RCLEAN CLEAN_PLUGINS

DISTCLEAN: CLEAN
	$(RM) src/Objects/* 2>/dev/null || :
	$(RM) misc/doxygen/Doxyfile
	$(RM) misc/man/itexmacs.1
	$(RM) misc/scripts/ifig2ps
	$(RM) misc/scripts/itexmacs
	$(RM) src/System/config.h
	$(RM) src/System/tm_configure.hpp
	$(RM) src/makefile
	$(RM) TeXmacs/examples/plugins/dynlink/Makefile
	$(RM) config.cache
	$(RM) config.log
	$(RM) config.status
	$(RM) Makefile

SVN_CLEAN:
	$(RM) -r .svn
	$(RM) -r */.svn
	$(RM) -r */*/.svn
	$(RM) -r */*/*/.svn
	$(RM) -r */*/*/*/.svn
	$(RM) -r */*/*/*/*/.svn
	$(RM) -r */*/*/*/*/*/.svn
	$(RM) -r */*/*/*/*/*/*/.svn	
	$(RM) -r */*/*/*/*/*/*/*/.svn

DISTR_CLEAN: DISTCLEAN SVN_CLEAN

.PHONY: CLEAN_DOC RDISTR DISTR RCLEAN SCLEAN DISTCLEAN SVN_CLEAN DISTR_CLEAN

###############################################################################
# Miscellaneous targets
###############################################################################

TOUCH:
	$(TOUCH) */*.make
	$(TOUCH) */*/*.hpp
	$(TOUCH) */*/*/*.hpp
	$(TOUCH) */*.cpp
	$(TOUCH) */*/*.cpp
	$(TOUCH) */*/*/*.cpp
	$(TOUCH) */*/*/*/*.cpp

STRIP:
	$(STRIP) $(tmdir)/bin/texmacs.bin
	$(STRIP) $(tmdir)/lib/*.$(so) 2>/dev/null || >/dev/null
	$(STRIP) $(tmdir)/plugins/*/bin/* 2>/dev/null || >/dev/null

ACCESS_FLAGS:
	$(MKDIR) -p $(tmdir)/bin
	$(MKDIR) -p $(tmdir)/lib
	$(CHMOD) -R go+rX *
	$(CHMOD) -R go+x $(tmdir)/bin
	$(CHMOD) -R go+x $(tmdir)/lib

.PHONY: TOUCH STRIP ACCESS_FLAGS
